package cn.jd.order.service.impl;

import cn.jd.basic.config.BussinessException;
import cn.jd.basic.util.PageList;
import cn.jd.order.domain.OrderPetAcquisition;
import cn.jd.order.domain.OrderPetAcquisitionLog;
import cn.jd.order.dto.OderPetDto;
import cn.jd.order.mapper.OrderPetAcquisitionLogMapper;
import cn.jd.order.mapper.OrderPetAcquisitionMapper;
import cn.jd.order.service.IOrderPetAcquisitionService;
import cn.jd.basic.service.impl.BasicServiceImpl;
import cn.jd.org.domain.Employee;
import cn.jd.pet.domain.Pet;
import cn.jd.pet.domain.SearchMasterMsg;
import cn.jd.pet.mapper.PetMapper;
import cn.jd.pet.mapper.SearchMasterMsgMapper;
import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Date;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author jd
 * @since 2022-11-01
 */
@Service
@Transactional
public class OrderPetAcquisitionServiceImpl extends BasicServiceImpl<OrderPetAcquisition> implements IOrderPetAcquisitionService {
   @Autowired
   private OrderPetAcquisitionMapper orderPetAcquisitionMapper;
   @Autowired
   private SearchMasterMsgMapper searchMasterMsgMapper;
   @Autowired
   private PetMapper petMapper;
   @Autowired
   private OrderPetAcquisitionLogMapper orderPetAcquisitionLogMapper;
    /**
     * 取消订单
     * @param id
     * @return
     */
    @Override
    public void cancel(Long id, Employee employee) {
        // 查询当前订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(id);
        if(orderPetAcquisition==null){
            throw  new BussinessException("当前订单不存在");
        }
        // 修改订单状态
        orderPetAcquisition.setShopId(null);
        orderPetAcquisition.setState(-1);
        orderPetAcquisitionMapper.update(orderPetAcquisition);
        // 查询寻主表
        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        if(searchMasterMsg==null){
            throw  new BussinessException("当前信息不存在");
        }
        searchMasterMsg.setState(1);
        searchMasterMsg.setShopId(null);
        searchMasterMsgMapper.update(searchMasterMsg);
       // 保存日志
        OrderPetAcquisitionLog log = new OrderPetAcquisitionLog();
        log.setEmpId(employee.getId());
        log.setNote("员工取消订单");
        log.setPetId(orderPetAcquisition.getPetId());
        orderPetAcquisitionLogMapper.save(log);
    }

    /**
     * 确认订单
     * @param dto
     * @return
     */
    @Override
    public void confirm(OderPetDto dto) {
        // 查询当前订单
        OrderPetAcquisition orderPetAcquisition = orderPetAcquisitionMapper.findById(dto.getId());
        if(orderPetAcquisition==null){
            throw  new BussinessException("当前订单不存在");
        }


        SearchMasterMsg searchMasterMsg = searchMasterMsgMapper.findById(orderPetAcquisition.getSearchMasterMsgId());
        if(searchMasterMsg==null){
            throw  new BussinessException("当前信息不存在");
        }
        searchMasterMsg.setState(3);
        searchMasterMsgMapper.update(searchMasterMsg);
        // 保存宠物数据
        Pet pet = new Pet();
        pet.setName(searchMasterMsg.getName());
        pet.setCostprice(dto.getMoney().toString());
        pet.setTypeId(1L);
        pet.setShopId(searchMasterMsg.getShopId());
        pet.setSearchMasterMsgId(searchMasterMsg.getId());
        pet.setNote(searchMasterMsg.getNote());
        petMapper.save(pet);
        // 修改订单状态
        orderPetAcquisition.setState(1);
        orderPetAcquisition.setPrice(dto.getMoney());
        orderPetAcquisition.setPetId(pet.getId());
        orderPetAcquisitionMapper.update(orderPetAcquisition);
    }



}
